﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class Department
    {
        private ISessionFactory _sessionFactory;
        public Department()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.Department)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public ArrayList GetListByStatus(string status)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from Department where Status =?").AddEntity(typeof(DataObjects.Department));
                query.SetString(0, status);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public ArrayList GetListByParentId(string parentId)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from Department where Parent_Id =?").AddEntity(typeof(DataObjects.Department));
                query.SetString(0, parentId);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public DataObjects.Department GetObjectByPK(string deptId)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Department where Department_ID =?").AddEntity(typeof(DataObjects.Department));
                query.SetString(0, deptId);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.Department)arrTemp[0];
                }
                return null;
            }
        }

        public int GetMaxID()
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Department order by cast( Department_Id as float ) desc").AddEntity(typeof(DataObjects.Department));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                DataObjects.Department oDept;

                if (arrTemp.Count > 0)
                {
                    oDept = (DataObjects.Department)arrTemp[0];
                    if (oDept != null)
                    {
                        return int.Parse(oDept.DepartmentId);
                    }
                }
                return 0;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.Department oDept)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oDept);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.Department oDept)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oDept);
                tx.Commit();		        
            }
        }
        public void DeleteByPK(string deptId)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DataObjects.Department oDept = (DataObjects.Department)session.Load(typeof(DataObjects.Department), deptId);
                session.Delete(oDept);
                tx.Commit();
            }
        }
    }
}
